#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/3/13 15:01
# @Author  : Swper
# @Site    : 
# @File    : views.py
# @Software: PyCharm

from flask import render_template, redirect,url_for,request,flash,session,jsonify,g
from app.admin import admin
from app.admin.forms import LoginForm,RoleForm,AddUserForm,RoleEditForm,AssetsForm
# from app.models import *
from app.admin.models import *
from app.decorators import login_required
from app.exts import db



@admin.route('/')
def index():
    return render_template('admin/index.html')


@admin.route('/login/',methods=['GET','POST'])
def login():
    form = LoginForm()
    if form.validate_on_submit():
        data = form.data
        user = User.query.filter_by(name=data["account"]).first()
        if not user.check_password(data["pwd"]):
            flash("密码错误")
            return redirect(url_for("admin.login"))
        session["admin"] = data["account"]
        return redirect(request.args.get("next") or url_for("admin.index"))
    return render_template('admin/login.html',form=form)


@admin.route('/logout/')
def logout():
    # del session['user_id']
    session.clear()
    return redirect(url_for('admin.login'))

@admin.route('/pwd/')
def pwd():
    return render_template('admin/pwd.html')

@admin.route('/role_add/',methods=['GET','POST'])
def role_add():
    form = RoleForm()
    if form.validate_on_submit():
        data = form.data
        rolename = Role.query.filter_by(name=data["name"]).count()
        if rolename == 1:
            flash("名称已经存在！","error")
            return redirect(url_for('admin.role_add'))
        rolename = Role(
            name = data["name"]
        )
        db.session.add(rolename)
        db.session.commit()
        flash("添加角色名称成功！","ok")
        return redirect(url_for("admin.role_list"))
    return render_template('admin/role_add.html',form=form)

#角色列表
@admin.route('/role_list/')
def role_list():
    rolelist = Role.query.all()
    return render_template('admin/role_list.html',rolelist=rolelist)

#角色删除
@admin.route('/role_list/del/<int:id>/',methods=['GET'])
def RoleDel(id=None):
    role = Role.query.filter_by(id=id).first_or_404()
    db.session.delete(role)
    db.session.commit()
    flash("删除角色成功！",'ok')
    return redirect(url_for('admin.role_list'))

#角色编辑
@admin.route('/role_list/edit/<int:id>/',methods=['GET','POST'])
def Role_edit(id=None):
    form = RoleEditForm()
    role = Role.query.get_or_404(id)
    if form.validate_on_submit():
        data = form.data
        role_count = Role.query.filter_by(name=data['name']).count()
        if role.name != data['name'] and role_count ==1:
            flash("名称已经存在！",'error')
            return redirect(url_for('admin.Role_edit',id=id))
        role.name = data['name']
        db.session.add(role)
        db.session.commit()
        flash("修改角色成功！",'ok')
        return redirect(url_for('admin.Role_edit',id=id))
    return render_template('admin/role_edit.html',form=form,role=role)





#用户列表
# @login_required
# @admin.route('/userlist')
# def user_list():
#     return render_template('admin/user_list.html')

@login_required
@admin.route('/user_view/')
def user_view():
    return render_template('admin/user_view.html')

@admin.route('/auth_add/')
def auth_add():
    return render_template('admin/auth_add.html')

@admin.route('/auth_list/')
def auth_list():
    return render_template('admin/auth_list.html')

#添加用户
@admin.route('/admin_add/',methods=['GET','POST'])
def admin_add():
    form = AddUserForm()
    if form.validate_on_submit():
        data = form.data
        user_name = User.query.filter_by(name=data["name"]).count()
        if user_name == 1:
            flash("用户名已经存在！","error")
            return redirect(url_for('admin.admin_add'))

        #添加列对应关系，插入到数据库中
        data=User(name=data['name'],pwd=data['pwd'],email=data['email'])
        db.session.add(data)
        db.session.commit()

        flash("添加用户成功！","ok")
        redirect(url_for("admin.admin_add"))
    return render_template('admin/admin_add.html',form=form)

#用户删除
@admin.route('/admin_list/del/<int:id>/',methods=['GET'])
def UserDel(id=None):
    user = User.query.filter_by(id=id).first_or_404()
    db.session.delete(user)
    db.session.commit()
    flash("删除用户成功！",'ok')
    return redirect(url_for('admin.admin_list'))

#用户列表
@admin.route('/admin_list/')
def admin_list():
    userlist = User.query.all()
    return render_template('admin/admin_list.html',userlist=userlist)

@admin.route('/loginlog_list/')
def loginlog_list():
    return render_template('admin/loginlog_list.html')

@admin.route('/oplog_list/')
def oplog_list():
    return render_template('admin/oplog_list.html')


@admin.route('/project_add/')
def project_add():
    return render_template('admin/project_add.html')

@admin.route('/project_list/')
def project_list():
    return render_template('admin/project_list.html')

@admin.route('/assets_add/')
def assets_add():
    return render_template('admin/assets_add.html')

#添加资产分组
@admin.route('/assets/groupadd/',methods=['GET','POST'])
def AssetsGroupAdd():
    form = AssetsForm()
    if form.validate_on_submit():
        data = form.data
        grouptype = GroupType.query.filter_by(name=data["name"]).count()
        if grouptype == 1:
            flash("分组名称已经存在！", "error")
            return redirect(url_for('admin.AssetsGroupAdd'))
        groupname = GroupType(
            name=data["name"]
        )
        db.session.add(groupname)
        db.session.commit()
        flash("添加分组名称成功！", "ok")
        return redirect(url_for("admin.AssetsGroupList"))
    return render_template('admin/assets_group.html', form=form)

#资产分组列表
@admin.route('/assets/grouplist/')
def AssetsGroupList():
    result = GroupType.query.all()
    return render_template('admin/assets_grouplist.html',result=result)



@admin.route('/assets_list/')
def assets_list():
    return render_template('admin/assets_list.html')

@admin.route('/code_add/')
def code_add():
    return render_template('admin/code_add.html')

@admin.route('/code_list/')
def code_list():
    return render_template('admin/code_list.html')


